package com.ybc.chinese.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.ybc.chinese.R;
import com.ybc.chinese.fragment.GalleryImageFragment;
import com.ybc.chinese.utils.Contants;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * <p>图片浏览<p/>
 * Created by Yubch on 17/2/20.
 */
public class GalleryActivity extends BaseActivity {

    @Bind(R.id.pager)
    ViewPager pager;

    private List<String> images;
    private int position;
    GalleryAdapter adapter;
    private boolean isPortrait;

    public static Intent newIntent(Context context, List<String> images, int position, boolean isPortrait) {
        Intent intent = new Intent();
        intent.setClass(context, GalleryActivity.class);
        intent.putStringArrayListExtra(Contants.INTENT_LIST, (ArrayList<String>) images);
        intent.putExtra(Contants.INTENT_POSITION, position);
        intent.putExtra(Contants.INTENT_IS_PORTRAIT, isPortrait);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        ButterKnife.bind(this);
        isPortrait = getIntent().getBooleanExtra(Contants.INTENT_IS_PORTRAIT, false);
        if(isPortrait) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        images = getIntent().getStringArrayListExtra(Contants.INTENT_LIST);
        position = getIntent().getIntExtra(Contants.INTENT_POSITION, 0);
        adapter = new GalleryAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        pager.setCurrentItem(position);
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    private class GalleryAdapter extends FragmentPagerAdapter {

        public GalleryAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public Fragment getItem(int position) {
            return GalleryImageFragment.newInstance(images.get(position));
        }

    }
}
